package com.laoye.service.impl;

import com.laoye.Exception.exception.BaseException;
import com.laoye.annotation.Log;
import com.laoye.bean.Cart;
import com.laoye.mapper.CartMapper;
import com.laoye.service.CartService;
import com.laoye.util.ThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;


    @Cacheable(value = "cart",
            key = "'cartList_'+T(com.laoye.util.ThreadLocal).getCurrentId()",
            unless = "#result == null || #result.isEmpty()")
    @Override
    public List<Cart> getCart() {
        return cartMapper.getCart(ThreadLocal.getCurrentId());
    }


    @CacheEvict(
            value = {"cart", "cartCount"},
            key = "'cartList_'+T(com.laoye.util.ThreadLocal).getCurrentId()" // 同时清理列表缓存和数量缓存
    )
    @Override
    @Log(value="添加商品到购物车")
    public void addToCart(Cart cart) {
        if (cart== null){
            throw new BaseException(401,"数据错误，无法添加到购物车");
        }
        cartMapper.addToCart(cart);
    }


    @CacheEvict(value = {"cart", "cartCount"},
            key = "'cartList_'+T(com.laoye.util.ThreadLocal).getCurrentId()")
    @Override
    @Log("从购物车移除商品")
    public void deleteCart(Integer prod_id) {
        if (prod_id == null){
            throw new BaseException(401,"商品ID不存在，无法移除购物车");
        }
        cartMapper.deleteCart(prod_id, ThreadLocal.getCurrentId());
    }


    @CacheEvict(value = {"cart", "cartCount"},
            key = "'cartList_'+T(com.laoye.util.ThreadLocal).getCurrentId()")
    @Override
    @Log("增加购买商品数量")
    public void increaseCount(Integer prod_id) {
        if (prod_id == null){
            throw new BaseException(401,"商品不存在，无法增加购买数量");
        }
        cartMapper.increaseCount(prod_id, ThreadLocal.getCurrentId());
    }


    @CacheEvict(value = {"cart", "cartCount"},
            key = "'cartList_'+T(com.laoye.util.ThreadLocal).getCurrentId()")
    @Override
    @Log("减少购买商品数量")
    public void decreaseCount(Integer prod_id) {
        if (prod_id == null){
            throw new BaseException(401,"商品不存在，无法减少购买数量");
        }
        cartMapper.decreaseCount(prod_id, ThreadLocal.getCurrentId());
    }


    @Cacheable(
            value = "cartCount",
            key = "'totalCount_' + T(com.laoye.util.ThreadLocal).getCurrentId() ",
            unless = "#result == null"
    )
    @Override
    public Integer getCount() {
        return cartMapper.getCount(ThreadLocal.getCurrentId());
    }


    @Override
    @Cacheable(
            value = "cartCount",
            key = "'prodCount_' +T(com.laoye.util.ThreadLocal).getCurrentId() + '_' + #prod_id",
            unless = "#result == null"
    )
    public Integer getProductCount(Integer prod_id) {
        if (prod_id == null){
            throw new BaseException(401,"商品不存在，无法获取购物车商品数量");
        }
        return cartMapper.getProductCount(ThreadLocal.getCurrentId(), prod_id);
    }


    @CacheEvict(
            value = {"cart", "cartCount"},
            key = "'cartList_'+T(com.laoye.util.ThreadLocal).getCurrentId()"
    )
    @Override
    @Log("更新购物车商品数量")
    public void updateCount(Integer count, Integer prod_id) {
        if (prod_id == null){
            throw new BaseException(401,"商品不存在，无法更新购物车商品数量");
        }
        if (count <= 0){
            throw new BaseException(401,"商品数量不能小于1");
        }
        cartMapper.updateCount(count, prod_id, ThreadLocal.getCurrentId());
    }
}
